package com.group2.view;
import static model.netBase.IP;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.*;

import model.*;
public class ShopAddFrame extends  JFrame {
	JTextField name;
	JTextField price;
	JTextField kind;
	JTextField surplus;
//	JFrame jf1=new JFrame();
	JPanel panel1 = new JPanel(null);// 第一个JPanel
	// JComboBox id;


	public ShopAddFrame() {
		init();
	}

	public void init() {
		this.setSize(300, 450);
		this.setResizable(false);
		panel1.setBounds(0, 0, 550, 450);
		Font d = new Font("黑体", Font.BOLD, 17);
		Font f = new Font("黑体", Font.PLAIN, 15);
		
		JLabel icon = new JLabel();
		icon.setIcon(new javax.swing.ImageIcon(UserLogOnFrame.class.getResource("/images/admin.png")));
		icon.setBounds(50, 50, 30, 30);
		
		JLabel goodstitle = new JLabel("请您添加商品信息：");
		JLabel goodsname = new JLabel("品名：");
		name = new JTextField();

		JLabel goodskind = new JLabel("种类：");
		kind = new JTextField();
		JLabel goodsprice = new JLabel("价格：");
		price = new JTextField();
		JLabel goodsurplus = new JLabel("剩余量：");
		surplus = new JTextField();

		JButton submit = new JButton("确认");
		submit.setFocusPainted(false);
		JButton reset = new JButton("重置");
		reset.setFocusPainted(false);

		goodstitle.setBounds(90, 45, 200, 40);
		goodstitle.setFont(d);

		goodsname.setBounds(50, 140, 100, 20);
		goodsname.setFont(f);
		name.setBounds(120, 140, 100, 20);
		name.setFont(f);
		
		
		goodskind.setBounds(50, 180, 100, 20);
		goodskind.setFont(f);
		kind.setBounds(120, 180, 100, 20);
		kind.setFont(f);
		
		goodsprice.setBounds(50, 220, 100, 20);
		goodsprice.setFont(f);
		price.setBounds(120, 220, 100, 20);
		price.setFont(f);
		
		goodsurplus.setBounds(50, 260, 100, 20);
		goodsurplus.setFont(f);
		surplus.setBounds(120, 260, 100, 20);
		surplus.setFont(f);
		
		submit.setBounds(70, 320, 70, 30);
		submit.setFont(f);
		reset.setBounds(150, 320, 70, 30);
		reset.setFont(f);

		panel1.add(icon);
	
		panel1.add(goodstitle);
		panel1.add(goodsname);
		panel1.add(name);
		panel1.add(goodskind);
		panel1.add(kind);
		panel1.add(goodsprice);
		panel1.add(price);
		panel1.add(goodsurplus);
		panel1.add(surplus);
		panel1.add(submit);
		panel1.add(reset);
		
		this.getContentPane().add(panel1);
		this.setVisible(true);
		// 居中显示
		this.setLocationRelativeTo(null);

		reset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				name.setText("");
				kind.setText("");
				price.setText("");
				surplus.setText("");
			}
		});

		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String image="/images/"+name.getText()+".png";
				try {
					Socket socket=new Socket(IP(),2000);
					socket.setSoTimeout(3000);
					//构建IO
					OutputStream os = socket.getOutputStream();
					BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
					bw.write("55\n"+
							name.getText()+'\n'+
							kind.getText()+'\n'+
							surplus.getText()+'\n'+
							price.getText()+'\n'+
							image+'\n');
					bw.flush();
					socket.close();
					JOptionPane.showMessageDialog(null, "添加成功！");
				} catch (UnknownHostException ex) {
					JOptionPane.showMessageDialog(null, "添加失败！");
					throw new RuntimeException(ex);
				} catch (IOException ex) {
					JOptionPane.showMessageDialog(null, "添加失败！");
					throw new RuntimeException(ex);
				}
			}
		});
	}

	public static void main(String[] args) {
		new ShopAddFrame();
	}
}